package com.handsoninfo;

import java.awt.*;
import java.util.List;

public class RGBCounter {
    private final List<RGBColor> colors;

    public RGBCounter(List<RGBColor> colors) {
        this.colors = colors;
    }

    public int count(List<RGBColor> target, double threshold) {
        int count = 0;
        for (RGBColor color : colors) {
            for (RGBColor targetColor: target) {
                double distance = euclideanDistance(color, targetColor);
                if (distance < threshold) {
                    count++;
                    break;
                }

            }
        }
        return count;
    }

    private double euclideanDistance(RGBColor color1, RGBColor color2) {
        double rDiff = color1.getRed() - color2.getRed();
        double gDiff = color1.getGreen() - color2.getGreen();
        double bDiff = color1.getBlue() - color2.getBlue();
        return Math.sqrt(rDiff * rDiff + gDiff * gDiff + bDiff * bDiff);
    }
}

class RGBColor {
    private final int red;
    private final int green;
    private final int blue;

    public RGBColor(int red, int green, int blue) {
        this.red = red;
        this.green = green;
        this.blue = blue;
    }

    public RGBColor(int rgb) {
        this.red = (rgb >> 16) & 0xFF;
        this.green = (rgb >> 8) & 0xFF;
        this.blue = rgb & 0xFF;
    }

    public int getRed() {
        return red;
    }

    public int getGreen() {
        return green;
    }

    public int getBlue() {
        return blue;
    }

    public int toInt() {
        return (red << 16) | (green << 8) | blue;
    }

    public Color toColor() { return new Color(red, green, blue); }
}
